﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Ven_Metas_CabDAO
    {
        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();

        public List<T_META> lista(int id_general_det) {

            List<T_META> list = new List<T_META>();
            try
            {
                var q = from c in ContextoDeObjs.T_VEN_METAS_CAB
                        from t in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        where c.ID_GENERAL_DET == t.ID_GENERAL_DET
                        && c.ID_GENERAL_DET == id_general_det
                        select new T_META
                        {
                            ID_UBIC_VEND_CAB=c.ID_UBIC_VEND_CAB,
                            Descripcion= c.Descripcion,
                            IND_COD_TIPO_BIEN_SERV=c.IND_COD_TIPO_BIEN_SERV,
                            DF_COD_TIPO_BIEN_SERV=c.DF_COD_TIPO_BIEN_SERV,
                            IND_NroRuedas=c.IND_NroRuedas,
                            ID_TIPO=c.ID_TIPO
                        };


                list = q.ToList();
            }
            catch
            {
                
            }
            return list;

        
        }




        public string Lista_vendedor_ofocina(int id_ubic_vend_cab) {

            IEnumerable<Object> list = new List<Object>();
            string resultado = "";
            try
            {


                var q = from tvm in ContextoDeObjs.T_VEN_METAS_CAB
                        from tgc in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        from tgd in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        where tgd.ID_GENERAL_CAB == tgc.ID_GENERAL_CAB && tgd.ID_GENERAL_DET == tvm.ID_TIPO
                        && tgc.NOM_TABLA == "T_TIPO_METAS" && tvm.ID_UBIC_VEND_CAB == id_ubic_vend_cab

                        select new { 
                        tgd.DES_CORTA
                        
                        };

                //list = q.ToList();
                
                foreach (var elementos in q)
                {
                    resultado= elementos.DES_CORTA;
                }
            }
            catch{               
               
            }

            return resultado;
        
        
        }


        public IEnumerable<Object> ListaMetas() {

            IEnumerable<Object> list = new List<Object>();
            try
            {

                var q = from c in ContextoDeObjs.T_VEN_METAS_CAB
                        from td in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        from tc in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        where tc.ID_GENERAL_CAB == td.ID_GENERAL_CAB
                        && c.ID_GENERAL_DET == td.ID_GENERAL_DET
                        select new
                        {
                            c.ID_UBIC_VEND_CAB,
                            c.Descripcion,
                            c.IND_NroRuedas,
                            DES_AREA = td.DES_CORTA,
                            c.ID_TIPO_VALOR,
                            DES_TIPO = ((from ta in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                                         where ta.ID_GENERAL_DET == c.ID_TIPO
                                         select new
                                         {

                                             ta.DES_CORTA
                                         }).FirstOrDefault().DES_CORTA),
                            DES_TIPO_VALOR = ((from f in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                                             where f.ID_GENERAL_DET == c.ID_TIPO_VALOR
                                             select new {f.DES_LARGA }).FirstOrDefault().DES_LARGA)

                        };
                list = q.ToList();
            }
            catch{}


            return list;
        
        }



        public IEnumerable<Object> listaTipoMeta() {

            IEnumerable<Object> list = new List<Object>();
            try
            {

                var q = from c in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        from d in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        where c.ID_GENERAL_CAB == d.ID_GENERAL_CAB
                        && c.NOM_TABLA == "T_TIPO_METAS"
                        && c.IND_ACTIVO==true
                        select new { 
                        d.ID_GENERAL_DET,
                        d.DES_CORTA,
                        d.DES_LARGA                       
                        
                        };


                list = q.ToList();

            }
            catch 
            {
                
               
            }

            return list;
        }









        public bool Insertar(T_VEN_METAS_CAB Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_VEN_METAS_CAB", Obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public bool Modificar(T_VEN_METAS_CAB obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_VEN_METAS_CAB", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }








        public bool ModificarCAB(int num_ped, string descripcion,int id_general_det,int? id_tipo,int id_tipo_valor)
        {
            try
            {
                T_VEN_METAS_CAB li = ContextoDeObjs.T_VEN_METAS_CAB.First(i => i.ID_UBIC_VEND_CAB == num_ped);
                li.Descripcion = descripcion;
                li.ID_GENERAL_DET = id_general_det;
                li.ID_TIPO = id_tipo;
                li.ID_TIPO_VALOR = id_tipo_valor;
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }





        public int UltimoNumero()
        {
            int max = 0;

            try
            {
                max = ContextoDeObjs.T_VEN_METAS_CAB.Select(x => x.ID_UBIC_VEND_CAB).Max();

            }
            catch (Exception)
            {
                return max;
            }

            return max;

        }


        public IEnumerable<Object> ListaTipoValor() {

            IEnumerable<Object> list = new List<Object>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        join s in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET on c.ID_GENERAL_CAB equals s.ID_GENERAL_CAB
                        where c.NOM_TABLA == "T_TIPO_META_VALOR"
                        select new
                        {
                            s.ID_GENERAL_DET,
                            s.DES_LARGA,
                        };
                list = q.ToList();
            }
            catch (Exception e)
            {
                
                throw e;
            }

            return list;
        }




        public String RetorTipoValor(int ID_UBIC_VEND_CAB)
        {
            try
            {
                var queryText = String.Format("SELECT  tmtgd.DES_LARGA FROM T_VEN_METAS_CAB tcmc INNER JOIN T_MAE_TABLA_GENERAL_DET tmtgd ON  TCMC.ID_TIPO_VALOR=TMTGD.ID_GENERAL_DET WHERE tcmc.ID_UBIC_VEND_CAB={0}", ID_UBIC_VEND_CAB);
                var lisr = ContextoDeObjs.ExecuteStoreQuery<String>(queryText).ToList();
                return lisr.FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
